package com.itheima.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.itheima.mapper.ReportDataMapper;
import com.itheima.pojo.HotSetmeal;
import com.itheima.pojo.ReportData;
import com.itheima.service.ReportDataService;
import com.itheima.utils.DateUtil;
import com.itheima.utils.FileUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.poi.ss.usermodel.Workbook;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


@DubboService
public class ReportDataServiceImpl implements ReportDataService {

    @Resource
    private ReportDataMapper reportDataMapper;

    @Resource
    private ReportDataService reportDataService;

    @Override
    public ReportData findReportData() {
        //查看多sql执行时间
        long s1 = System.currentTimeMillis();
        //采用多线程完成sql查询
        ExecutorService executorService = Executors.newFixedThreadPool(11);
        Future<Integer> findTodayNewMember = executorService.submit(() -> {
            return reportDataMapper.findTodayNewMember();
        });
        Future<Integer> findTotalMember = executorService.submit(() -> {
            return reportDataMapper.findTotalMember();
        });
        Future<Integer> findThisWeekNewMember = executorService.submit(() -> {
            return reportDataMapper.findThisWeekNewMember();
        });
        Future<Integer> findThisMonthNewMember = executorService.submit(() -> {
            return reportDataMapper.findThisMonthNewMember();
        });
        Future<Integer> findThisWeekOrderNumber = executorService.submit(() -> {
            return reportDataMapper.findThisWeekOrderNumber();
        });
        Future<Integer> findTodayOrderNumber = executorService.submit(() -> {
            return reportDataMapper.findTodayOrderNumber();
        });
        Future<Integer> findTodayVisitsNumber = executorService.submit(() -> {
            return reportDataMapper.findTodayVisitsNumber();
        });
        Future<Integer> findThisWeekVisitsNumber = executorService.submit(() -> {
            return reportDataMapper.findThisWeekVisitsNumber();
        });
        Future<Integer> findThisMonthOrderNumber = executorService.submit(() -> {
            return reportDataMapper.findThisMonthOrderNumber();
        });
        Future<Integer> findThisMonthVisitsNumber = executorService.submit(() -> {
            return reportDataMapper.findThisMonthVisitsNumber();
        });
        Future<List<HotSetmeal>> findHostSetmeal = executorService.submit(() -> {
            return reportDataMapper.findHostSetmeal();
        });


        ReportData reportData = new ReportData();
        reportData.setReportDate(DateUtil.formatDate(new Date()));
        try {
            reportData.setThisMonthNewMember(findThisMonthNewMember.get());
            reportData.setThisWeekNewMember(findThisWeekNewMember.get());
            reportData.setThisWeekOrderNumber(findThisWeekOrderNumber.get());
            reportData.setTodayNewMember(findTodayNewMember.get());
            reportData.setTodayOrderNumber(findTodayOrderNumber.get());
            reportData.setTodayVisitsNumber(findTodayVisitsNumber.get());
            reportData.setTotalMember(findTotalMember.get());
            reportData.setThisWeekVisitsNumber(findThisWeekVisitsNumber.get());
            reportData.setThisMonthOrderNumber(findThisMonthOrderNumber.get());
            reportData.setThisMonthVisitsNumber(findThisMonthVisitsNumber.get());
            reportData.setHotSetmeals(findHostSetmeal.get());
        } catch (Exception e) {
            e.printStackTrace();
        }

        long s2 = System.currentTimeMillis();
        System.out.println("查询时间:" + (s2 - s1));

        return reportData;
    }

    @Override
    public String importExcel() throws Exception{
        ReportData reportData = reportDataService.findReportData();
        TemplateExportParams params = new TemplateExportParams(
                "https://awei-health.oss-cn-shanghai.aliyuncs.com/report_template.xlsx");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("reportDate",reportData.getReportDate() );
        map.put("todayNewMember", reportData.getTodayNewMember());
        map.put("totalMember", reportData.getTotalMember());
        map.put("thisWeekNewMember", reportData.getThisWeekNewMember());
        map.put("thisMonthNewMember", reportData.getThisMonthNewMember());
        map.put("todayOrderNumber", reportData.getTodayOrderNumber());
        map.put("todayVisitsNumber", reportData.getTodayVisitsNumber());
        map.put("thisWeekOrderNumber", reportData.getThisWeekOrderNumber());
        map.put("thisWeekVisitsNumber", reportData.getThisWeekVisitsNumber());
        map.put("thisMonthOrderNumber", reportData.getThisMonthOrderNumber());
        map.put("thisMonthVisitsNumber", reportData.getThisMonthVisitsNumber());
        List<HotSetmeal> hotSetmeals = reportData.getHotSetmeals();
        List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
        for (int i = 0; i < hotSetmeals.size(); i++) {
            HotSetmeal hotSetmeal = hotSetmeals.get(i);
            Map<String, String> lm = new HashMap<String, String>();
            lm.put("name",hotSetmeal.getName()+"");
            lm.put("counts",hotSetmeal.getCounts()+"");
            lm.put("zhanbi",hotSetmeal.getZhanbi()+"");
            listMap.add(lm);
        }

        map.put("list", listMap);

        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        File savefile = new File("E:/excel/");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        String fileName = "报表"+System.currentTimeMillis() +".xls";
        FileOutputStream fos = new FileOutputStream(savefile.getPath()+"/"+fileName);
        workbook.write(fos);
        fos.close();

        FileInputStream fileInputStream = new FileInputStream(savefile.getPath()+"/"+fileName);
        String key = FileUtil.upload(fileInputStream, fileName);
        return key;
    }

}
